﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.Entity;
using Kendo.Mvc.UI;
using Kendo.Mvc.Extensions;
using TS2015.Models;
using System.Web.Script.Serialization;

namespace TS2015.Areas.Admin.Controllers
{
    public class QLDangKyController : Controller
    {
        TS2015Context db = new TS2015Context();

        // GET: Admin/QLDangKy
        //Hệ đào tạo
        public ActionResult HeDaoTao()
        {
            Session["current_url"] = Request.Url.AbsoluteUri;
            if (!Request.IsAjaxRequest())
                return View();

            return PartialView();
        }
        public ActionResult EditingPopup_ReadHDT([DataSourceRequest] DataSourceRequest request)
        {
            return Json(db.TbHeDaoTaos.ToDataSourceResult(request));
        }

        //[ValidateAntiForgeryToken]
        public ActionResult EditingPopup_CreateHDT([DataSourceRequest] DataSourceRequest request, TbHeDaoTao data)
        {
            if (data != null && ModelState.IsValid)
            {
                db.TbHeDaoTaos.Add(data);
                db.SaveChanges();
            }
            return Json(ModelState.ToDataSourceResult());
        }
        public ActionResult EditingPopup_UpdateHDT([DataSourceRequest] DataSourceRequest request, TbHeDaoTao data)
        {
            if (data != null && ModelState.IsValid)
            {
                db.Entry(data).State = EntityState.Modified;
                db.SaveChanges();
            }
            return Json(ModelState.ToDataSourceResult());
        }
        public ActionResult EditingPopup_DestroyHDT([DataSourceRequest] DataSourceRequest request, TbHeDaoTao data)
        {
            if (data != null)
            {
                TbHeDaoTao itemdata = db.TbHeDaoTaos.First(item => item.MaHeDaoTao == data.MaHeDaoTao);
                db.TbHeDaoTaos.Remove(itemdata);
                db.SaveChanges();
            }
            return Json(ModelState.ToDataSourceResult());
        }

        //[OutputCache(Duration=600)]
        public JsonResult GetKhoa()
        {
            return Json(db.TbKhoas.ToList(), JsonRequestBehavior.AllowGet);
        }
        //Khoa
        public ActionResult Khoa()
        {
            if (!Request.IsAjaxRequest())
                return View();

            return PartialView();
        }
        public ActionResult EditingPopup_ReadKhoa([DataSourceRequest] DataSourceRequest request)
        {
            return Json(db.TbKhoas.ToDataSourceResult(request));
        }

        public ActionResult EditingPopup_CreateKhoa([DataSourceRequest] DataSourceRequest request, TbKhoa data)
        {
            if (data != null && ModelState.IsValid)
            {
                db.TbKhoas.Add(data);
                db.SaveChanges();
            }
            return Json(ModelState.ToDataSourceResult());
        }

        public ActionResult EditingPopup_UpdateKhoa([DataSourceRequest] DataSourceRequest request, TbKhoa data)
        {
            if (data != null && ModelState.IsValid)
            {
                db.Entry(data).State = EntityState.Modified;
                db.SaveChanges();
            }
            return Json(ModelState.ToDataSourceResult());
        }
        public ActionResult EditingPopup_DestroyKhoa([DataSourceRequest] DataSourceRequest request, TbKhoa data)
        {
            if (data != null)
            {
                TbKhoa itemdata = db.TbKhoas.First(item => item.MaKhoa == data.MaKhoa);
                db.TbKhoas.Remove(itemdata);
                db.SaveChanges();
            }
            return Json(ModelState.ToDataSourceResult());
        }
         //[ChildActionOnly]
        [HttpPost]
        [ActionName("Insert")]
        public ActionResult Insert1()
        {
            return PartialView();
        }
        public ActionResult Insert()
        {
            return PartialView();
        }
        #region Các thao tác với Ngành
        public ActionResult Nganh()
        {
            // Lấy danh sách khoa để tham chiếu khóa ngoại
            ViewBag.TbKhoa = db.TbKhoas.ToList();

            if (!Request.IsAjaxRequest())
                return View();

            return PartialView();
        }
        public ActionResult EditingPopup_ReadNganh([DataSourceRequest] DataSourceRequest request)
        {
            var data = db.TbNganhs;

            return Json(data.ToDataSourceResult(request));
        }

        public ActionResult EditingPopup_CreateNganh([DataSourceRequest] DataSourceRequest request, TbNganh data)
        {
            if (data != null && ModelState.IsValid)
            {
                db.TbNganhs.Add(data);
                db.SaveChanges();
            }
            return Json(ModelState.ToDataSourceResult());
        }
        public ActionResult EditingPopup_UpdateNganh([DataSourceRequest] DataSourceRequest request, TbNganh data)
        {
            if (data != null && ModelState.IsValid)
            {
                db.Entry(data).State = EntityState.Modified;
                db.SaveChanges();
            }
            return Json(ModelState.ToDataSourceResult());
        }
        public ActionResult EditingPopup_DestroyNganh([DataSourceRequest] DataSourceRequest request, TbNganh data)
        {
            if (data != null)
            {
                TbNganh itemdata = db.TbNganhs.First(item => item.MaNganh == data.MaNganh);
                db.TbNganhs.Remove(itemdata);
                db.SaveChanges();
            }
            return Json(ModelState.ToDataSourceResult());
        }

        #endregion

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}